/*
                  InterfaceLobby Class Implementation
*/
#include "InterfaceLobby.h"

InterfaceLobby::InterfaceLobby(InterfaceType NewType)
{
    Type = NewType;
}

InterfaceLobby::~InterfaceLobby()
{

}

int InterfaceLobby::Initialise()
{
    frmPlayer1 = new PlayersetupFrame;
    frmPlayer1->Initialise(Point(50, 50), 1, 1);
    frmPlayer1->setBackground("data//interface//gamelobby//playersetup.png");
    frmPlayer1->setVisible(true);
    frmPlayer1->setFixed(true);
    frmPlayer1->lstType->setSelectedItem("Local");

    frmPlayer2 = new PlayersetupFrame;
    frmPlayer2->Initialise(Point(50, 150), 1, 1);
    frmPlayer2->setBackground("data//interface//gamelobby//playersetup.png");
    frmPlayer2->setVisible(true);
    frmPlayer2->setFixed(true);
    frmPlayer2->lstType->setSelectedItem("CPU");

    frmPlayer3 = new PlayersetupFrame;
    frmPlayer3->Initialise(Point(50, 250), 1, 1);
    frmPlayer3->setBackground("data//interface//gamelobby//playersetup.png");
    frmPlayer3->setVisible(true);
    frmPlayer3->setFixed(true);
    frmPlayer3->lstType->setSelectedItem("CPU");

    frmPlayer4 = new PlayersetupFrame;
    frmPlayer4->Initialise(Point(50, 350), 1, 1);
    frmPlayer4->setBackground("data//interface//gamelobby//playersetup.png");
    frmPlayer4->setVisible(true);
    frmPlayer4->setFixed(true);
    frmPlayer4->lstType->setSelectedItem("CPU");

    frmPlayer5 = new PlayersetupFrame;
    frmPlayer5->Initialise(Point(50, 450), 1, 1);
    frmPlayer5->setBackground("data//interface//gamelobby//playersetup.png");
    frmPlayer5->setVisible(true);
    frmPlayer5->setFixed(true);
    frmPlayer5->lstType->setSelectedItem("CPU");

    frmPlayer6 = new PlayersetupFrame;
    frmPlayer6->Initialise(Point(50, 550), 1, 1);
    frmPlayer6->setBackground("data//interface//gamelobby//playersetup.png");
    frmPlayer6->setVisible(true);
    frmPlayer6->setFixed(true);
    frmPlayer6->lstType->setSelectedItem("CPU");

    frmOptionbar = new Frame;
    frmOptionbar->Initialise(Point(0, 0), ConfigFile::iCurrentWindowResX, 50);
    frmOptionbar->setVisible(true);
    frmOptionbar->setFixed(true);
    frmOptionbar->WindowAlign(ALIGN_LEFT, GetWindowSize());
    frmOptionbar->WindowAlign(ALIGN_BOTTOM, GetWindowSize());

    btnBack = new Button;
    btnBack->Initialise(Point(50, 0), "Back to Menu", "data//fonts//bold.ttf", 14, "data//interface//button.png", "data//interface//button_c.png", GetSDLColor(255, 255, 255));

    btnStart = new Button;
    btnStart->Initialise(Point(ConfigFile::iCurrentWindowResX - 250, 0), "Start Game", "data//fonts//bold.ttf", 14, "data//interface//button.png", "data//interface//button_c.png", GetSDLColor(255, 255, 255));

    frmOptionbar->AddWidget(btnBack, "btnBack");
    frmOptionbar->AddWidget(btnStart, "btnStart");

    frmMap = new Frame;
    frmMap->Initialise(Point(0, 0), 1, 1);
    frmMap->setBackground("data//interface//gamelobby//mapselectionbg.png");
    frmMap->setVisible(true);
    frmMap->setFixed(true);
    frmMap->WindowAlign(ALIGN_RIGHT, GetWindowSize());
    frmMap->MoveRelative(-50, 50);

    imgPreview = new Imagebox;
    imgPreview->Initialise("", Point(41, 41));

    imgBorder = new Imagebox;
    imgBorder->Initialise("data//interface//gamelobby//mapborder.png", Point(35, 35));

    lstMaplist = new Listbox;
    lstMaplist->Initialise(Point(115, 280), 18, "data//fonts//bold.ttf", 155, 0x313131, GetSDLColor(255, 255, 255));
    lstMaplist->SetListItems(MapInfo::GetMapNames());

    lblMap = new Label;
    lblMap->Initialise(Point(15, 280), "data//fonts//bold.ttf", 14, "Map: ", GetSDLColor(255, 255, 255));

    lblPlayers = new Label;
    lblPlayers->Initialise(Point(15, 330), "data//fonts//bold.ttf", 14, "Recommended Players: ", GetSDLColor(255, 255, 255));

    lblSize = new Label;
    lblSize->Initialise(Point(15, 380), "data//fonts//bold.ttf", 14, "Size: ", GetSDLColor(255, 255, 255));

    lblGametype = new Label;
    lblGametype->Initialise(Point(15, 430), "data//fonts//bold.ttf", 14, "Game Type: ", GetSDLColor(255, 255, 255));

    vector<string> Gametypes;
    Gametypes.push_back("Score Match");
    Gametypes.push_back("Death Match");

    lstGametype = new Listbox;
    lstGametype->Initialise(Point(115, 430), 18, "data//fonts//bold.ttf", 155, 0x313131, GetSDLColor(255, 255, 255));
    lstGametype->SetListItems(Gametypes);

    lblScorelimit = new Label;
    lblScorelimit->Initialise(Point(15, 480), "data//fonts//bold.ttf", 14, "Score Limit: ", GetSDLColor(255, 255, 255));

    txtScorelimit = new TextBox;
    txtScorelimit->Initialise(Point(115, 480), "data//fonts//gui.ttf", 18, 6, "data//interface//gamelobby//textbox.png", GetSDLColor(255, 255, 255), 500);
    txtScorelimit->setText("50");
    txtScorelimit->setValidInput("0123456789");

    lblStartSize = new Label;
    lblStartSize->Initialise(Point(15, 530), "data//fonts//bold.ttf", 14, "Start Size: ", GetSDLColor(255, 255, 255));

    txtStartSize = new TextBox;
    txtStartSize->Initialise(Point(115, 530), "data//fonts//gui.ttf", 18, 6, "data//interface//gamelobby//textbox.png", GetSDLColor(255, 255, 255), 500);
    txtStartSize->setText("5");
    txtStartSize->setValidInput("0123456789");

    lblFoodCount = new Label;
    lblFoodCount->Initialise(Point(15, 580), "data//fonts//bold.ttf", 14, "Food Count: ", GetSDLColor(255, 255, 255));

    txtFoodCount = new TextBox;
    txtFoodCount->Initialise(Point(115, 580), "data//fonts//gui.ttf", 18, 6, "data//interface//gamelobby//textbox.png", GetSDLColor(255, 255, 255), 500);
    txtFoodCount->setText("3");
    txtFoodCount->setValidInput("0123456789");

    RefreshMapInfo();

    frmMap->AddWidget(imgBorder, "imgBorder");
    frmMap->AddWidget(imgPreview, "imgPreview");
    frmMap->AddWidget(lblMap, "lblMap");
    frmMap->AddWidget(lblPlayers, "lblPlayers");
    frmMap->AddWidget(lblSize, "lblSize");
    frmMap->AddWidget(lblGametype, "lblGametype");
    frmMap->AddWidget(lblScorelimit, "lblScorelimit");
    frmMap->AddWidget(txtScorelimit, "txtScorelimit");
    frmMap->AddWidget(lblStartSize, "lblStartSize");
    frmMap->AddWidget(txtStartSize, "txtStartSize");
    frmMap->AddWidget(lblFoodCount, "lblFoodCount");
    frmMap->AddWidget(txtFoodCount, "txtFoodCount");
    frmMap->AddWidget(lstGametype, "lstGametype");
    frmMap->AddWidget(lstMaplist, "lstMaplist");

    frmgLobby.AddFrame(frmPlayer1, "frmPlayer1");
    frmgLobby.AddFrame(frmPlayer2, "frmPlayer2");
    frmgLobby.AddFrame(frmPlayer3, "frmPlayer3");
    frmgLobby.AddFrame(frmPlayer4, "frmPlayer4");
    frmgLobby.AddFrame(frmPlayer5, "frmPlayer5");
    frmgLobby.AddFrame(frmPlayer6, "frmPlayer6");
    frmgLobby.AddFrame(frmOptionbar, "frmOptionbar");
    frmgLobby.AddFrame(frmMap, "frmMap");

    imgBackground.Initialise(ConfigFile::strBGPath, Point(0, 0));

    RefreshPlayerInfo();
}

InterfaceType InterfaceLobby::HandleInput(const SDL_Event& event)
{
    frmgLobby.parseInput(event);

    return CheckWidgetEvents();
}

InterfaceType InterfaceLobby::CheckWidgetEvents()
{
    GUIEvent guievent;

    while (frmOptionbar->PollEvent(guievent) == 0)
    {
        switch (guievent.getEventType())
        {
            case GUIEVENT_LEFTMOUSEUP:
            {
                if (guievent.getSource() == "btnBack")
                {
                    return MENU;
                }

                if (guievent.getSource() == "btnStart")
                {
                    // Score game is the default type
                    GameType gameType = GAMETYPE_SCORE;

                    string strType = lstGametype->getSelectedItem();

                    if (strType == "Score Match")
                    {
                        gameType = GAMETYPE_SCORE;
                    }
                    else if (strType == "Death Match")
                    {
                        gameType = GAMETYPE_KILL;
                    }

                    if (SnakeEngine::GameSetup(lstMaplist->getSelectedItem(), lexical_cast<int>(txtStartSize->getText()),
                                                gameType, lexical_cast<int>(txtFoodCount->getText()), lexical_cast<int>(txtScorelimit->getText())) == -1)
                    {
                        return NONE;
                    }

                    for (int i = 1; i <= 6; i++)
                    {
                        AddPlayerToGame(i);
                    }

                    SnakeEngine::setLocalPlayer("Player");

                    SnakeEngine::StartGame();

                    return INGAME;
                }
            }
            break;
        }
    }

    while (frmMap->PollEvent(guievent) == 0)
    {
        switch (guievent.getEventType())
        {
            case GUIEVENT_LEFTMOUSEDOWN:
            {
                if (guievent.getSource() == "lstMaplist")
                {
                    MapInfo::SetCurrentMap(lstMaplist->getSelectedItem());

                    RefreshMapInfo();
                }

                if (guievent.getSource() == "lstGametype")
                {
                    if (lstGametype->getSelectedItem() == "Score Match")
                    {
                        lblScorelimit->setText("Score Limit:");
                    }
                    else // death match
                    {
                        lblScorelimit->setText("Kill Limit:");
                    }
                }
            }
        }
    }

    PlayersetupFrame *checkFrame = NULL;

    for (int i = 1; i <= 6; i++)
    {
        checkFrame = GetPlayerFrame(i);

        if (checkFrame == NULL)
        {
            continue;
        }

        while (checkFrame->PollEvent(guievent) == 0)
        {
            switch (guievent.getEventType())
            {
                case GUIEVENT_LEFTMOUSEDOWN:
                {
                    if (checkFrame->lstType->getSelectedItem() == "Local")
                    {
                        for (int x = 1; x <= 6; x++)
                        {
                            PlayersetupFrame *compareFrame = GetPlayerFrame(x);

                            if (compareFrame == NULL)
                            {
                                continue;
                            }

                            if (compareFrame->lstType->getSelectedItem() == "Local" && x != i)
                            {
                                compareFrame->lstType->setSelectedItem("CPU");
                            }
                        }
                    }

                    RefreshPlayerInfo();
                }
                break;
            }
        }
    }

    return NONE;
}

void InterfaceLobby::AddPlayerToGame(const int& iSlot)
{
    PlayersetupFrame *temp = GetPlayerFrame(iSlot);
    bool bIsCPU = false;

    if (temp == NULL)
    {
        return;
    }

    if (temp->chkPlaying->getChecked() == false)
    {
        return;
    }

    if (temp->lstType->getSelectedItem() != "Player")
    {
        bIsCPU = true;
    }
    else
    {
        bIsCPU = false;
    }

    SnakeEngine::AddPlayer(temp->lstType->getSelectedItem(), bIsCPU);
}

PlayersetupFrame* InterfaceLobby::GetPlayerFrame(const int& id)
{
    switch (id)
    {
        case 1:
            return frmPlayer1;
        break;

        case 2:
            return frmPlayer2;
        break;

        case 3:
            return frmPlayer3;
        break;

        case 4:
            return frmPlayer4;
        break;

        case 5:
            return frmPlayer5;
        break;

        case 6:
            return frmPlayer6;
        break;
    }

    return NULL;
}

void InterfaceLobby::RefreshPlayerInfo()
{
    for (int x = 1; x <= 6; x++)
    {
        PlayersetupFrame *compareFrame = GetPlayerFrame(x);

        if (compareFrame == NULL)
        {
            continue;
        }

        if (compareFrame->lstType->getSelectedItem() == "CPU")
        {
            compareFrame->setName("CPU");
        }
    }
}

void InterfaceLobby::RefreshMapInfo()
{
    MapData *Map = MapInfo::GetCurrentMap();

    if (Map != NULL)
    {
        imgPreview->setImage("data//maps//" + Map->strName + "//preview.png");

        lblPlayers->setText("Recommended Players: " + Map->strRecommendedPlayers);
        lblSize->setText("Size: " + Map->strSize);

        lstMaplist->setSelectedItem(Map->strName);
    }
}

int InterfaceLobby::Draw(SDL_Surface *surface)
{
    imgBackground.BlitToSurface(surface);
    frmgLobby.BlitToSurface(surface);
}
